package org.zk.controller;

import com.github.pagehelper.Page;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zk.entity.Board;
import org.zk.entity.Topic;
import org.zk.service.BoardService;
import org.zk.service.TopicService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明:topic资讯相关的控制器
 *
 * @姚文斌
 * @create 2019-11-18 下午 5:06
 */
@Controller
public class TopicController {
    @Autowired
    @Setter
    @Getter
    private TopicService topicService;
    @Autowired
    @Setter
    @Getter
    private BoardService boardService;
    /**
     * ========================= 方式1.异步得到所有Topic资讯(同时得到所有的board版块) ===================
     * ========================= PS:此方式已经做过了Redis缓存                             =========
     */

    @RequestMapping("/allTopic")
    @ResponseBody
    public Map allTopic(int pageSize, int pageNum,Topic topic){
//        System.out.println(topic.getTitle());
//        Map map=new HashMap();
//        List<Topic> topicList=topicService.allTopic(topic);
//        List<Board> boardList=boardService.getAllBoard();
//        map.put("topicList",topicList);
//        map.put("boardList",boardList);
//        return map;
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("topic", topic);
        //利用pageHelper中的Page类盛放结果集，再把page对象放到map中返回给异步页面
        Page page = topicService.queryLikeTopics(map);
        System.out.println(page.getResult());
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;//把map返回给异步页面
    }
    /**
     * ========================= 方式2.先请求控制器，由控制器把页面和数据一起转发 ===================
     */


    @RequestMapping("/toList")
    @ResponseBody
    public ModelAndView editPage(Topic topic) {
        Map map=new HashMap();
        if (topic.getTitle()==null){
            topic.setTitle("");
        }
        List<Topic> topicList=topicService.allTopic(topic);
        List<Board> boardList=boardService.getAllBoard();
        map.put("topicList",topicList);
        map.put("boardList",boardList);
        //通过ModelAndView把封装好的数据转发到指定的页面
        return new ModelAndView("article-list", map);
    }

    //3.方式一的删除
    @RequestMapping("/del")
    @ResponseBody
    public Map del(Topic topic){
        topicService.del(topic);
        Map map=new HashMap();
        map.put("data","已删除");
        return map;//返回给回调函数的数据类型必须是Map
    }
    //4.方式二的删除：
    @RequestMapping("/del2")
    @ResponseBody
    public Map del2(Topic topic){
        System.out.println(topic.getTopicid());
        topicService.del(topic);
        Map map=new HashMap();
        map.put("data","已删除");
        return map;//返回给回调函数的数据类型必须是Map
    }




}
